home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\STACK.C < prev    next >
C/C++ Source or Header  |  1994-12-28  |  6KB  |  353 lines

  1. /*
  2.  * stack.c - does the handling of stack functions
  3.  *
  4.  * written by matthew green
  5.  *
  6.  * copyright (C) 1993.
  7.  */
  8.  
  9. #ifndef lint
  10. static    char    rcsid[] = "@(#)$Id: stack.c,v 1.13 1994/10/14 23:14:23 mrg Stab $";
  11. #endif
  12.  
  13. #include "irc.h"
  14.  
  15. #include "stack.h"
  16. #include "window.h"
  17. #include "hook.h"
  18. #include "ircaux.h"
  19. #include "output.h"
  20. #include "list.h"
  21.  
  22. static    OnStack    *on_stack = NULL;
  23. static  AliasStack *alias_stack = NULL;
  24. static    AliasStack *assign_stack = NULL;
  25. static    SetStack *set_stack = NULL;
  26.  
  27. static    AliasStack *alias_get __P((char *, int));
  28. static    AliasStack *alias_stack_find __P((char *, int));
  29. static    void    alias_stack_add __P((AliasStack *, int));
  30.  
  31. static    void
  32. do_stack_on(type, args)
  33.     int    type;
  34.     char    *args;
  35. {
  36.     char    foo[4];
  37.     int    len, cnt, i, which = 0;
  38.     Hook    *list;
  39.     NumericList *nhook, *nptr, *ntmp;
  40.  
  41.     if (!on_stack && (type == STACK_POP || type == STACK_LIST))
  42.     {
  43.         say("ON stack is empty!");
  44.         return;
  45.     }
  46.     if (!args || !*args)
  47.     {
  48.         say("Missing event type for STACK ON");
  49.         return;
  50.     }
  51.     len = strlen(args);
  52.     for (cnt = 0, i = 0; i < NUMBER_OF_LISTS; i++)
  53.     {
  54.         if (!my_strnicmp(args, hook_functions[i].name, len))
  55.         {
  56.             if (strlen(hook_functions[i].name) == len)
  57.             {
  58.                 cnt = 1;
  59.                 which = i;
  60.                 break;
  61.             }
  62.             else
  63.             {
  64.                 cnt++;
  65.                 which = i;
  66.             }
  67.         }
  68.         else if (cnt)
  69.             break;
  70.     }
  71.     if (!cnt)
  72.     {
  73.         if (is_number(args))
  74.         {
  75.             which = atoi(args);
  76.             if (which < 1 || which > 999)
  77.             {
  78.                 say("Numerics must be between 001 and 999");
  79.                 return;
  80.             }
  81.             which = -which;
  82.         }
  83.         else
  84.         {
  85.             say("No such ON function: %s", args);
  86.             return;
  87.         }
  88.     }
  89.     if (which < 0)
  90.     {
  91.         sprintf(foo, "%3.3u", -which);
  92.         if ((nhook = (NumericList *) find_in_list(&numeric_list, foo, 0))
  93.                 != NULL)
  94.             list = nhook->list;
  95.         else
  96.             list = NULL;
  97.     }
  98.     else
  99.         list = hook_functions[which].list;
  100.     if (type == STACK_PUSH)
  101.     {
  102.         OnStack    *new;
  103.  
  104.         if (list == NULL)
  105.         {
  106.             say("The ON %s list is empty", args);
  107.             return;
  108.         }
  109.         new = (OnStack *) new_malloc(sizeof(OnStack));
  110.         new->next = on_stack;
  111.         on_stack = new;
  112.         new->which = which;
  113.         new->list = list;
  114.         if (which < 0)
  115.         {
  116.             if (nhook == numeric_list)
  117.             {
  118.                 numeric_list = nhook->next;
  119.                 new_free(&nhook->name);
  120.                 new_free(&nhook);
  121.                 return;
  122.             }
  123.             for (nptr = numeric_list; nptr;
  124.                     ntmp = nptr, nptr = nptr->next)
  125.             {
  126.                 if (nptr == nhook)
  127.                 {
  128.                     ntmp->next = nptr->next;
  129.                     new_free(&nptr->name);
  130.                     new_free(&nptr);
  131.                     return;
  132.                 }
  133.             }
  134.         }
  135.         else
  136.             hook_functions[which].list = NULL;
  137.         return;
  138.     }
  139.     else if (type == STACK_POP)
  140.     {
  141.         OnStack    *p, *tmp;
  142.  
  143.         for (p = on_stack; p; tmp = p, p = p->next)
  144.         {
  145.             if (p->which == which)
  146.             {
  147.                 if (p == on_stack)
  148.                     on_stack = p->next;
  149.                 else
  150.                     tmp->next = p->next;
  151.                 break;
  152.             }
  153.         }
  154.         if (!p)
  155.         {
  156.             say("No %s on the stack", args);
  157.             return;
  158.         }
  159.         if (which < 0 && nhook || hook_functions[which].list)
  160.             remove_hook(which, NULL, 0, 0, 1);    /* free hooks */
  161.         if (which < 0)
  162.         {
  163.             if ((nptr = (NumericList *) find_in_list(&numeric_list,
  164.                     foo, 0)) == NULL)
  165.             {
  166.                 nptr = (NumericList *) new_malloc(sizeof(NumericList));
  167.                 nptr->name = NULL;
  168.                 nptr->list = p->list;
  169.                 malloc_strcpy(&nptr->name, foo);
  170.                 add_to_list(&numeric_list, nptr);
  171.             }
  172.             else
  173.                 add_to_list(&numeric_list->list, p->list);
  174.         }
  175.         else
  176.             hook_functions[which].list = p->list;
  177.         return;
  178.     }
  179.     else if (type == STACK_LIST)
  180.     {
  181.         int    slevel = 0;
  182.         OnStack    *osptr;
  183.  
  184.         for (osptr = on_stack; osptr; osptr = osptr->next)
  185.             if (osptr->which == which)
  186.             {
  187.                 Hook    *hptr;
  188.  
  189.                 slevel++;
  190.                 say("Level %d stack", slevel);
  191.                 for (hptr = osptr->list; hptr; hptr = hptr->next)
  192.                     show_hook(hptr, args);
  193.             }
  194.  
  195.         if (!slevel)
  196.             say("The STACK ON %s list is empty", args);
  197.         return;
  198.     }
  199.     say("Unknown STACK ON type ??");
  200. }
  201.  
  202. static    void
  203. do_stack_alias(type, args, which)
  204.     int    type;
  205.     char    *args;
  206.     int    which;
  207. {
  208.     char    *name;
  209.     AliasStack    *aptr,
  210.             **aptrptr;
  211.  
  212.     if (which == STACK_DO_ALIAS)
  213.     {
  214.         name = "ALIAS";
  215.         aptrptr = &alias_stack;
  216.     }
  217.     else
  218.     {
  219.         name = "ASSIGN";
  220.         aptrptr = &assign_stack;
  221.     }
  222.     if (!*aptrptr && (type == STACK_POP || type == STACK_LIST))
  223.     {
  224.         say("%s stack is empty!", name);
  225.         return;
  226.     }
  227.  
  228.     if (STACK_PUSH == type)
  229.     {
  230.         aptr = alias_get(args, which);
  231.         if ((AliasStack *) 0 == aptr)
  232.         {
  233.             say("No such %s %s", name, args);
  234.             return;
  235.         }
  236.         if (aptrptr)
  237.             aptr->next = *aptrptr;
  238.         *aptrptr = aptr;
  239.         return;
  240.     }
  241.     if (STACK_POP == type)
  242.     {
  243.         aptr = alias_stack_find(args, which);
  244.         if ((AliasStack *) 0 == aptr)
  245.         {
  246.             say("%s is not on the %s stack!", args, name);
  247.             return;
  248.         }
  249.         alias_stack_add(aptr, which);
  250.         return;
  251.     }
  252.     if (STACK_LIST == type)
  253.     {
  254.         say("stack list is not implimented yet");
  255.         return;
  256.     }
  257.     say("Unknown STACK type ??");
  258. }
  259.  
  260. static    void
  261. do_stack_set(type, args)
  262.     int    type;
  263.     char    *args;
  264. {
  265. }
  266.  
  267. /*
  268.  * alias_get: this returns a point to an `AliasStack' structure that
  269.  * has be extracted from the current aliases, and removed from that
  270.  * list.
  271.  */
  272. static    AliasStack* alias_get(args, which)
  273.     char    *args;
  274.     int    which;
  275. {
  276.     return (AliasStack *) 0;
  277. }
  278.  
  279. /*
  280.  * alias_stack_find: this returns the pointer to the struct with the
  281.  * most recent alias for `args' in the stack.
  282.  */
  283. static    AliasStack*
  284. alias_stack_find(args, which)
  285.     char    *args;
  286.     int    which;
  287. {
  288.     return (AliasStack *) 0;
  289. }
  290.  
  291. /*
  292.  * alias_stack_add: this adds `aptr' to the alias/assign stack.
  293.  */
  294. static    void
  295. alias_stack_add(aptr, which)
  296.     AliasStack *aptr;
  297.     int which;
  298. {
  299.     return;
  300. }
  301.  
  302. extern    void
  303. stackcmd(command, args)
  304.     char    *command,
  305.         *args;
  306. {
  307.     char    *arg;
  308.     int    len, type;
  309.  
  310.     if ((arg = next_arg(args, &args)) != NULL)
  311.     {
  312.         len = strlen(arg);
  313.         if (!my_strnicmp(arg, "PUSH", len))
  314.             type = STACK_PUSH;
  315.         else if (!my_strnicmp(arg, "POP", len))
  316.             type = STACK_POP;
  317.         else if (!my_strnicmp(arg, "LIST", len))
  318.             type = STACK_LIST;
  319.         else
  320.         {
  321.             say("%s is unknown stack type", arg);
  322.             return;
  323.         }
  324.     }
  325.     else
  326.     {
  327.         say("Need operation for STACK");
  328.         return;
  329.     }
  330.     if ((arg = next_arg(args, &args)) != NULL)
  331.     {
  332.         len = strlen(arg);
  333.         if (!my_strnicmp(arg, "ON", len))
  334.             do_stack_on(type, args);
  335.         else if (!my_strnicmp(arg, "ALIAS", len))
  336.             do_stack_alias(type, args, STACK_DO_ALIAS);
  337.         else if (!my_strnicmp(arg, "ASSIGN", len))
  338.             do_stack_alias(type, args, STACK_DO_ASSIGN);
  339.         else if (!my_strnicmp(arg, "SET", len))
  340.             do_stack_set(type, args);
  341.         else
  342.         {
  343.             say("%s is not a valid STACK type");
  344.             return;
  345.         }
  346.     }
  347.     else
  348.     {
  349.         say("Need stack type for STACK");
  350.         return;
  351.     }
  352. }
  353.